package com.tansun.easycare.rule.codeblock.web;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.codeblock.model.CodeblockVO;
import com.tansun.easycare.rule.codeblock.service.RuleCodeblockService;
import com.tansun.easycare.rule.common.ResMessage;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;

@Controller
@RequestMapping(value = "/ruleProj/codeblock")
public class RuleCodeblockController extends RuleBaseController {

    @Autowired
    private RuleManageService ruleManageService;

    @Autowired
    private RuleCodeblockService ruleCodeblockService;

    /**
     * 跳转脚本列表
     */
    @RequestMapping("index")
    public  @ResponseBody ResponseTable<RuleManageVO> index(@RequestBody RuleManageVO param) throws Exception {
    	PageInfo<RuleManageVO> pageInfo = this.ruleManageService.findRuleManageVO(param.getPage(), param);
        ResponseTable<RuleManageVO>  resVo  = new  ResponseTable<RuleManageVO>(pageInfo);
        return resVo; 
    }

    /**
     * 跳转至代码块新增页面
     */
    @RequestMapping("toAdd")
    public String toAdd(String rankRelationId,ModelMap map) throws Exception {
        CodeblockVO vo = new CodeblockVO();
        RuleManage ruleManage = new RuleManage();
        ruleManage.setProjectCode(getCurrProjectCode());
        ruleManage.setRankRelationId(rankRelationId);
        vo.setRuleManage(ruleManage);
        map.put("model", vo);
        map.put("rankRelationId", rankRelationId);
        return "rule/codeblock/add";
    }

    /**
     * 跳转至代码块编辑页面
     *
     * @param code 代码块Code
     * @return 路径
     */
    @RequestMapping("toEdit")
    public String toEdit(String id, ModelMap map) throws Exception {
        CodeblockVO vo = this.ruleCodeblockService.findCodeblocks(id, this.getCurrProjectCode());
        String codeBody = StringEscapeUtils.escapeXml11(vo.getRuleCodeblock().getCodeBody());
        vo.getRuleCodeblock().setStrCodeBody(codeBody);
        map.put("model", vo);
        return "rule/codeblock/add";
    }
    
    /**
     * 获取代码块详情信息
     *
     * @param model 代码块信息
     * @return 是否成功对象，不成功则返回异常信息
     */
    @RequestMapping("detail")
    @ResponseBody
    public ResponseVO detail(@RequestBody RuleManageVO param) throws Exception {
    	ResponseVO msg = new ResponseVO();
    	msg.setStatus(ResponseVO.OK);
    	try {
	        CodeblockVO vo = this.ruleCodeblockService.findCodeblocks(param.getId(), param.getProjectCode());
	        String codeBody = StringEscapeUtils.escapeXml11(vo.getRuleCodeblock().getCodeBody());
	        vo.getRuleCodeblock().setStrCodeBody(codeBody);
	        msg.setData(vo);
    	}catch(Exception e) {
    		msg.setDescription(e.getMessage());
    		msg.setDesDetail(ExceptionUtils.getStackTrace(e));
    		msg.setStatus(ResponseVO.ERROR);
    	}
        return msg;
    }

    /**
     * 保存代码块信息
     *
     * @param model 代码块信息
     * @return 是否成功对象，不成功则返回异常信息
     */
    @RequestMapping("save")
    @ResponseBody
    public ResponseVO save(@RequestBody CodeblockVO model) throws Exception {
        return ruleCodeblockService.save(model);
    }

    /**
     * 编译代码块
     *
     * @param model 包含代码块代码信息
     * @return 是否成功对象，不成功则返回异常信息
     */
    @RequestMapping("compile")
    @ResponseBody
    public ResponseVO compile(@RequestBody CodeblockVO model) throws Exception {
    	ResponseVO msg = ruleCodeblockService.compileByFlow(model);
        return msg;
    }

    @RequestMapping("deleCodeBock")
    @ResponseBody
    public ResponseVO deleCodeBock(@RequestBody RuleManageVO param) {
    	ResponseVO msg = new ResponseVO();
    	msg.setStatus(ResponseVO.OK);
        try {
            this.ruleCodeblockService.delCodeBlock(param.getId());
        } catch (BizException e) {
    		msg.setDescription(e.getMessage());
    		msg.setDesDetail(ExceptionUtils.getStackTrace(e));
    		msg.setStatus(ResponseVO.ERROR);
        }
        return msg;
    }
}
